#!/bin/bash

base_dir="./"
log_dir="/var/log/yum"
data_dir="/data/mirrors"
log_rsync_status_file="${log_dir}/rsync_yum_list_status.log"
log_rsync_status_file_count=$(cat $log_rsync_status_file|grep -Ev "(^#|^$)"|wc -l)
job_dir_file="/data/mirrors/jobs.json"

>$job_dir_file
echo "[" >>$job_dir_file
for n in `seq 1 $log_rsync_status_file_count`
do
    DATE=$(date +%F)
    TIME=$(date +%T)
    ZONE="+0800"
    date_time="$DATE $TIME $ZONE"
    name=$(cat ${log_rsync_status_file}|grep -Ev "(^$|^#)"|awk -F "," 'NR=='$n'{print $1}'|xargs)    
    is_master=$(cat ${log_rsync_status_file}|grep -Ev "(^$|^#)"|awk -F "," 'NR=='$n'{print $2}'|xargs)
    STATUS=$(cat ${log_rsync_status_file}|grep -Ev "(^$|^#)"|awk -F "," 'NR=='$n'{print $3}'|xargs)
    last_update="$date_time"
    last_update_ts=$(cat ${log_rsync_status_file}|grep -Ev "(^$|^#)"|awk -F "," 'NR=='$n'{print $5}'|xargs)
    last_started="$date_time"
    last_started_ts=$(cat ${log_rsync_status_file}|grep -Ev "(^$|^#)"|awk -F "," 'NR=='$n'{print $7}'|xargs)
    last_ended="$date_time"
    last_ended_ts=$(cat ${log_rsync_status_file}|grep -Ev "(^$|^#)"|awk -F "," 'NR=='$n'{print $9}'|xargs)
    next_schedule="$date_time"
    next_schedule_ts=$(cat ${log_rsync_status_file}|grep -Ev "(^$|^#)"|awk -F "," 'NR=='$n'{print $11}'|xargs)
    upstream=$(cat ${log_rsync_status_file}|grep -Ev "(^$|^#)"|awk -F "," 'NR=='$n'{print $12}'|xargs)
    yum_data_dir=$(cat ${log_rsync_status_file}|grep -Ev "(^$|^#)"|awk -F "," 'NR=='$n'{print $14}'|xargs)
    if [ ! -d $yum_data_dir ];then
        size=$(cat ${log_rsync_status_file}|grep -Ev "(^$|^#)"|awk -F "," 'NR=='$n'{print $13}'|xargs)
    else
        yum_data_dir_size=$(du -sm ${yum_data_dir}|awk '{print $1}')
        size="${yum_data_dir_size} MB"
    fi

    if [ -f  ${data_dir}/.help/rsync_yum_${name}_status.html ];then
        \cp -f ${data_dir}/.help/rsync_yum_${name}_status.html ${data_dir}/.help/rsync_yum_${name}_status.html_source
        tail -10 ${data_dir}/.help/rsync_yum_${name}_status.html_source  > ${data_dir}/.help/rsync_yum_${name}_status.html
        > ${data_dir}/.help/rsync_yum_${name}_status.html_source
    fi

if [ $n == $log_rsync_status_file_count ];then
cat >>$job_dir_file<<EOF
    {
        "name":"${name}",
        "is_master":${is_master},
        "status":"${STATUS}",
        "last_update":"${last_update} +0800",
        "last_update_ts":${last_update_ts},
        "last_started":"${last_started} +0800",
        "last_started_ts":${last_started_ts},
        "last_ended":"${last_ended} +0800",
        "last_ended_ts":${last_ended_ts},
        "next_schedule":"${next_schedule} +0800",
        "next_schedule_ts":${next_schedule_ts},
        "upstream":"${upstream}",
        "size":"${size}"
    }
EOF
continue
fi

cat >>$job_dir_file<<EOF
    {
        "name":"${name}",
        "is_master":${is_master},
        "status":"${STATUS}",
        "last_update":"${last_update} +0800",
        "last_update_ts":${last_update_ts},
        "last_started":"${last_started} +0800",
        "last_started_ts":${last_started_ts},
        "last_ended":"${last_ended} +0800",
        "last_ended_ts":${last_ended_ts},
        "next_schedule":"${next_schedule} +0800",
        "next_schedule_ts":${next_schedule_ts},
        "upstream":"${upstream}",
        "size":"${size}"
    },
EOF


done
echo "]" >>$job_dir_file
